from flask import Flask, request, redirect, render_template, session

app = Flask(__name__)
app.secret_key = 'niedoqebowoefweihiowefihwf'


@app.route('/', methods=['GET'])
def asd():
    return redirect('/login')


@app.route('/login', methods=['GET', 'POST'])
def login():
    """
    request:
    """
    # request.args # 参数传递值
    # request.form # 表单传递值
    print(request)
    if request.method == 'GET':
        return render_template("login.html")
    username = request.form.get("username")
    pwd = request.form.get("pwd")
    if username == 'huhao' and pwd == '123456':
        # 用户信息存入sessions
        # 1.加盐加密session
        # flask将session存在cookie中
        session['userinfo'] = username
        return redirect("/index")
    else:
        return render_template('login.html', msg="登陆失败，请重新登陆")


@app.route('/index', methods=['GET'])
def index():
    if session.get('userinfo'):
        return render_template("index.html")
    else:
        return redirect("/login")

@app.route('/logout', methods=['GET'])
def logout():
    del session['userinfo']
    return redirect("/index")


if __name__ == '__main__':
    app.run(debug=True)